package com.example.test.imageswitcherview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

import com.example.test.R;

public class ImageSwitcherAActivity extends Activity implements ViewFactory,
		OnItemSelectedListener {
	private Integer[] mThumbIds = { R.drawable.thumb1, R.drawable.thumb2,
			R.drawable.thumb3, R.drawable.thumb4, R.drawable.thumb5,
			R.drawable.thumb6, R.drawable.thumb7, R.drawable.thumb8 };
	private Integer[] mImageIds = { R.drawable.sample1, R.drawable.sample2,
			R.drawable.sample3, R.drawable.sample4, R.drawable.sample5,
			R.drawable.sample5, R.drawable.sample6, R.drawable.sample7,
			R.drawable.sample8 };
	private ImageSwitcher imageSwitcher;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.imageswitcherview);
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcherview);
		imageSwitcher.setFactory(this);
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
		Gallery gallery = (Gallery) findViewById(R.id.galleryswitcherview);
		gallery.setAdapter(new ImageAdapter(this, mThumbIds, mImageIds));
		gallery.setOnItemSelectedListener(this);
	}

	@Override
	public View makeView() {
		ImageView imageView = new ImageView(this);
		imageView.setBackgroundColor(0xFF000000);
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return imageView;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		imageSwitcher.setImageResource(mImageIds[arg2]);

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}
}
